import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Spyer implements KeyListener,ActionListener{
	
	private SnakeEngine engine;
	public Spyer(SnakeEngine eng){
		engine = eng;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		//il timer attiva lo snakeEngine
		if (e.getActionCommand().equals("timer")) {
			engine.step();
		}else if(e.getActionCommand().equalsIgnoreCase("restart")){			
			engine.restart();
		}else if (e.getActionCommand().equals("looser")) {
			GameWindow.setTimerHop(150);
			engine.restart();
		}else if (e.getActionCommand().equals("notbad")) {
			GameWindow.setTimerHop(100);
			engine.restart();
		}else if (e.getActionCommand().equals("medium")) {
			GameWindow.setTimerHop(75);
			engine.restart();
		}else if (e.getActionCommand().equals("expert")) {
			GameWindow.setTimerHop(50);
			engine.restart();
		}else if (e.getActionCommand().equals("god")) {
			GameWindow.setTimerHop(25);
			engine.restart();
		}
		GameWindow.refreshGameDisplay();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		
		if(key == KeyEvent.VK_UP || key == KeyEvent.VK_W){
			engine.setMovement(SnakeEngine.UP);
		}else if (key == KeyEvent.VK_DOWN || key == KeyEvent.VK_S) {
				engine.setMovement(SnakeEngine.DOWN);
		}else if (key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) {
				engine.setMovement(SnakeEngine.LEFT);
		}else if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) {
				engine.setMovement(SnakeEngine.RIGHT);		
		
		}else if (key == KeyEvent.VK_SPACE || key == KeyEvent.VK_P){
			if(engine.isPaused())
				engine.start();
			else
				engine.pause();
		}else if (e.isControlDown() && key == KeyEvent.VK_R){
			engine.restart();
		}
		
	}
	@Override
	public void keyReleased(KeyEvent e) {}
	@Override
	public void keyTyped(KeyEvent e) {}

}
